package com.xidian.zhiancloud.service.emergencyManagement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.ReviewRecordMapper;
import com.xidian.zhiancloud.dao.RevisionRecordMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.model.ReviewRecord;
import com.xidian.zhiancloud.model.RevisionRecord;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.chemicalsManagement.DangerousChemicalStorageQO;
import com.xidian.zhiancloud.qo.emergencyManagement.ReviewRecordQO;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalStorageVO;
import com.xidian.zhiancloud.vo.emergencyManagement.ReviewRecordVO;
import com.xidian.zhiancloud.vo.emergencyManagement.RevisionRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReviewRecordService extends ServiceImpl<ReviewRecordMapper, ReviewRecord> implements IService<ReviewRecord> {
    @Autowired
    private ReviewRecordMapper mapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    public List<ReviewRecordVO> getList(Long id) {
        LambdaQueryWrapper<ReviewRecord> query = Wrappers.<ReviewRecord>lambdaQuery();
        query.eq(ReviewRecord::getEmergencyPlanId, id);
        List<ReviewRecord> list = mapper.selectList(query);
        List<ReviewRecordVO> ans = new LinkedList<>();
        list.forEach(t -> {
            ReviewRecordVO vo = new ReviewRecordVO();
            BeanUtils.copyProperties(t, vo);
            Organization organization = organizationMapper.selectById(vo.getOrganizationId());
            vo.setDepartmentName(organization == null ? "该部门已被删除" : organization.getName());
            String[] split = vo.getReviewerIds().split(",");
            vo.setReviewerNames(Arrays.stream(split).map(ids_s -> {
                SysUser tmp = sysUserMapper.selectById(ids_s);
                return tmp == null ? "该用户已被删除" : tmp.getName();
            }).collect(Collectors.toList()));
            ans.add(vo);
        });
        return ans;
    }

    public IPage<ReviewRecordVO> getListByQueryObject(ReviewRecordQO reviewRecordQO) {
        Page<ReviewRecordVO> page = new Page<>(reviewRecordQO.getCurrent(), reviewRecordQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        return mapper.getListByQO(page, reviewRecordQO);
    }


}
